﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockholmsStad.Web.UI.UIServiceReference;

namespace StockholmsStad.Web.UI.Controls
{
    /// <summary>
    /// Webcontrol för ett registreringsformulär för en person
    /// </summary>
    public partial class RegistrationPersonControl : UserControl
    {
        #region Properties

        /// <summary>
        /// Sex måste mellanlagras eftersom Page_Load inte är körd ännu
        /// </summary>
        private Sex? _personSex;

        private bool enabled = true;

        /// <summary>
        /// Personen som registreras
        /// </summary>
        public Person Person
        {
            get
            {
                Address postAddress = pnlAddressFields.Visible
                                          ? new Address
                                                {
                                                    City = txtPostName.Text,
                                                    CoAddress = txtCareOf.Text,
                                                    Country = txtCountry.Text,
                                                    StreetAddress = txtAddress.Text,
                                                    PostCode = txtPostNumber.Text,
                                                }
                                          : null;
                var person = new Person(txtSurName.Text, txtLastName.Text, txtPersonId.PersonalNumber,
                                        rblIsSwedish.SelectedValue.Equals("true", StringComparison.OrdinalIgnoreCase),
                                        txtNationality.Text,
                                        rblSex.SelectedValue.Equals(Sex.Male.ToString()),
                                        postAddress);
                return person;
            }
            set
            {
                if (value != null)
                {
                    setPostAddress(value.Address);
                    txtSurName.Text = value.FirstNames;
                    txtLastName.Text = value.LastName;
                    bool swedish = Person.IsSwedish(value.Nationality);
                    rblIsSwedish.SelectedValue = swedish ? "true" : "false";
                    txtNationality.Visible = !swedish;
                    txtNationality.Text = value.Nationality;
                    txtPersonId.PersonalNumber = value.PersonalNumberUI;
                    SetSex(value);
                }
            }
        }

        /// <summary>
        /// Ska addressfält visas för personen
        /// </summary>
        public bool ShowAddress
        {
            get { return pnlAddressFields.Visible; }
            set { pnlAddressFields.Visible = value; }
        }

        /// <summary>
        /// Ska registeringsinformationen vara skrivbar
        /// </summary>
        public bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                rblIsSwedish.Enabled = value;
                txtNationality.Enabled = value;
                rblSex.Enabled = value;
                txtPersonId.EnabledText = value;
                txtSurName.TextEnabled = value;
                txtLastName.TextEnabled = value;
                txtCareOf.TextEnabled = value;
                txtAddress.TextEnabled = value;
                txtPostNumber.TextEnabled = value;
                txtPostName.TextEnabled = value;
                txtCountry.TextEnabled = value;
            }
        }

        private void SetSex(Person value)
        {
            _personSex = value.Sex;
            if (rblSex.Items.Count != 0)
                rblSex.SelectedValue = _personSex.ToString();
        }

        #endregion

        /// <summary>
        /// Körs när sidan laddas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (rblSex.Items.Count == 0)
            {
                rblSex.Items.Add(new ListItem("Man", Sex.Male.ToString()));
                rblSex.Items.Add(new ListItem("Kvinna", Sex.Female.ToString()));
            }
            if (_personSex != null)
                rblSex.SelectedValue = _personSex.ToString();
        }

        private void setPostAddress(Address postAddress)
        {
            if (postAddress != null)
            {
                txtAddress.Text = postAddress.StreetAddress;
                txtCareOf.Text = postAddress.CoAddress;
                txtCountry.Text = postAddress.Country;
                txtPostName.Text = postAddress.City;
                txtPostNumber.Text = postAddress.PostCode;
            }
        }

        /// <summary>
        /// Användaren har förändrat värde för 'svensk medborgare' kryssruta
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rblIsSwedish_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtNationality.Visible = !rblIsSwedish.SelectedValue.Equals("true", StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Validerar att användaren fyller i nationalitet ifall han inte är folkborkförd i Sverige
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        protected void txtNationalityCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = rblIsSwedish.SelectedValue.Equals("true") || !string.IsNullOrEmpty(txtNationality.Text);
        }
    }
}